Skip to content

fix(recording): continue on libx264 when Media Foundation stops - #4490

Open
kyzer1023 wants to merge 5 commits into
CapSoftware:mainfrom
kyzer1023:fix/windows-mf-midtake-fallback
Open

kyzer1023 wants to merge 5 commits into
CapSoftware:mainfrom
kyzer1023:fix/windows-mf-midtake-fallback

Conversation

@kyzer1023

@kyzer1023 kyzer1023 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When the native Windows Media Foundation H.264 encoder stops mid-take in WindowsMuxer or WindowsCameraMuxer, Cap currently fails the encoder task instead of continuing the recording. This PR switches to libx264 on the existing MP4 video stream and take clock so the take completes with continuous A/V.

Changes

  • crates/enc-ffmpeg (base.rs, video/h264.rs, video/mid_take.rs): Add H264EncoderBuilder::build_software_on_stream(stream_index, last_stream_dts) and EncoderBase::continue_after(last_stream_dts) so libx264 continues writing to the existing stream without rebasing timestamps backwards. Includes regression tests covering the mid-take H.264 stream swap (mid_take.rs).
  • crates/mediafoundation-ffmpeg & crates/enc-mediafoundation: Expose the last written stream DTS across the Media Foundation muxer boundary and add EncoderFailureReason::Injected driven by CAP_MF_FAIL_AFTER_FRAMES for reproducible mid-take failure testing.
  • crates/recording (output_pipeline/win.rs, output_pipeline/frame_pacing.rs): On Media Foundation runtime failure in WindowsMuxer and WindowsCameraMuxer, open libx264 on the same stream, emit PipelineHealthEvent::EncoderRebuilt, and carry the last ScreenFrame into the software loop via frame_pacing::frame_to_encode so static-screen repeats continue without a multi-second gap across the switch.

Verification

Tested on Windows 11 with an AMD Radeon 780M (cap-gpui --release at 12d98a072, CAP_MF_FAIL_AFTER_FRAMES=30 for takes 1–2, unset for control; ~10.5 s Studio screen + audio takes):

Take Video duration Frames Audio duration (packets) Max video PTS gap Switch boundary
1. Injected MF fail @30, static screen 10.617 s 316 10.743 s (538) 0.1333 s (0.967 -> 1.100 s) last MF 1.100 s, first libx264 1.133 s
2. Injected MF fail @30, moving ticker 10.550 s 314 10.662 s (534) 0.1333 s (0.967 -> 1.100 s) last MF 1.100 s, first libx264 1.133 s
3. Control (MF only, static screen) 10.500 s 315 10.686 s (535) 0.0333 s no fallback (no x264 SEI)
  • All three takes finished with status Complete, zero ERROR logs, and playable video/audio.
  • Takes 1 and 2 logged Media Foundation H264 stopped mid-take; switching to libx264 at frames_encoded=30 and contain the x264 - core 164 SEI header; the control does not.
  • Carrying the last frame across the switch reduced the static-screen switch gap from 3.290 s (before 12d98a072) to 0.1333 s.
  • Note: Tested on AMD Radeon 780M via CAP_MF_FAIL_AFTER_FRAMES=30 (on AMD, Media Foundation is skipped unless the env hook is active). NVIDIA and Intel Windows hardware were not tested locally.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge, with both previous findings addressed.

Summary

The PR lets Windows screen and camera recordings switch from Media Foundation to libx264 without restarting the video timeline.

  • Software H.264 can continue an existing video track.
  • Screen recordings keep going when Media Foundation H.264 stops.
  • Camera recordings switch to software encoding when Media Foundation stops.

Reviews (2) · Last reviewed commit: "test: bound video spacing across the mid..." · Reviewed by Greptile

kyzer1023 and others added 3 commits November 23, 2026 16:38
A second encoder on the stream Media Foundation already wrote rebases
timestamps, and the mov muxer rejects the next packet.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
The software encoder stays on the existing stream and the take clock,
so the next packet cannot move backwards.

Co-authored-by: Kyzer  <kyzer1023@users.noreply.github.com>
A static capture sends no new frames. The software loop was starting
without the frame Media Foundation had been repeating, so those repeats
encoded nothing until the screen changed.
@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai

@vercel

vercel Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@kyzer1023 is attempting to deploy a commit to the Cap Software Inc Team on Vercel.

A member of the Team first needs to authorize it.

Comment on lines +85 to +89
assert!(
decoded.last_video_secs > 1.5,
"video timestamps restart at the switch, last pts {:.3}s",
decoded.last_video_secs
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Video gaps escape the test

The new test never checks video spacing or order. Checking that the largest video timestamp exceeds 1.5 seconds still passes if frames disappear around the switch or timestamps move backwards before recovering. Record consecutive video timestamps and assert their order, the largest gap across the switch, and the final difference between audio and video.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/enc-ffmpeg/src/video/mid_take.rs
Line: 85-89

Comment:
**Video gaps escape the test**

The new test never checks video spacing or order. Checking that the largest video timestamp exceeds 1.5 seconds still passes if frames disappear around the switch or timestamps move backwards before recovering. Record consecutive video timestamps and assert their order, the largest gap across the switch, and the final difference between audio and video.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 46ea27dd1: mid_take now records every video packet timestamp in order and asserts non-decreasing PTS, consecutive frame gaps (including across the switch) strictly under two frames, and final audio/video end-time diff within 100 ms.

End,
}

/// A repeat encodes the frame already carried. An empty carry drops it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Comment repeats the code

This sentence restates the two cases implemented by frame_to_encode below. AGENTS.md explicitly bans comments that narrate what the code does. Remove it or explain a non-obvious constraint instead. This repository requirement must be satisfied before merging.

Suggested change
/// A repeat encodes the frame already carried. An empty carry drops it.

Context Used: AGENTS.md (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/recording/src/output_pipeline/frame_pacing.rs
Line: 31

Comment:
**Comment repeats the code**

This sentence restates the two cases implemented by `frame_to_encode` below. `AGENTS.md` explicitly bans comments that narrate what the code does. Remove it or explain a non-obvious constraint instead. This repository requirement must be satisfied before merging.

```suggestion

```

**Context Used:** AGENTS.md ([source](https://github.com/capsoftware/cap/blob/main/AGENTS.md))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 46ea27dd1: removed the narrating doc comment on PacedPixels.

Record every video packet timestamp and reject a backwards step, a gap
wider than two frames, or an audio and video ending more than 100ms
apart. Drop the comment that restated frame_to_encode.
@kyzer1023

Copy link
Copy Markdown
Contributor Author

@greptileai

kyzer1023 added a commit to kyzer1023/Cap that referenced this pull request Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant